package com.huawei.controller;

import lombok.val;

import java.util.PriorityQueue;

/**
 * description
 *
 * @author cwx1134046
 * @since 2023-01-31
 */
public class MergeKLists {

    public static class ListNode {
         int val;
         ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public static ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>((o1,o2)->o1.val - o2.val);
        if(lists.length == 0) {
            return null;
        }
        for (ListNode node:lists) {
            if(node != null){
                queue.add(node);
            }
        }
        ListNode res = null;
        ListNode temp = null;
        while(!queue.isEmpty()){

            ListNode poll = queue.poll();
            if(temp == null){
                res = new ListNode();
                temp = res;
                temp.val = poll.val;
            }else   {
                temp.next  = poll;
                temp = temp.next;
            }
            ListNode next;
            if(( next =poll.next) != null){
                queue.add(next);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        //[[1,4,5],[1,3,4],[2,6]]
        int[][] arrs = {{}};
        ListNode[] nodes = new ListNode[arrs.length];
        int j = 0;
        for (int[] arr:arrs){
            ListNode listNode = new ListNode();
            nodes[j] = listNode;
            for (int i:arr) {
                if(listNode.val == 0){
                    listNode.val = i;
                }else{
                    listNode.next = new ListNode(i);
                    listNode =listNode.next;
                }
            }
            j++;
        }
        ListNode listNode = mergeKLists(nodes);
    }
}
